package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.ResetPayPassActivityView;
import com.wushi.lenovo.asia5b.app_di.profile.presenter.ResetPayPassActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.ChangeProfileEvent;
import com.wushi.lenovo.asia5b.app_model.profile.bean.ChangeProfileType;
import com.wushi.lenovo.asia5b.app_utils.WToastHelper;
import com.wushi.lenovo.asia5b.app_widget.pass.PayPwdEditText;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyuncai on 2019/3/1.
 * 忘记密码的充值密码界面
 */
public class ResetPayPassActivity extends BaseMvpActivity<
        ResetPayPassActivityView, ResetPayPassActivityPresenter> implements ResetPayPassActivityView {
    @BindView(R.id.paypwd_resetpaypass_new)
    PayPwdEditText paypwd_resetpaypass_new;
    @BindView(R.id.paypwd_resetpaypass_renew)
    PayPwdEditText paypwd_resetpaypass_renew;
    @BindView(R.id.tv_resetpaypass_renew_error)
    TextView tv_resetpaypass_renew_error;
    @BindView(R.id.bt_resetpaypass_save)
    Button bt_resetpaypass_save;
    private String otp;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_resetpaypass;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        otp = getIntent().getStringExtra("otp");
        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {
        paypwd_resetpaypass_new.initStyle(R.drawable.edit_num_bg, 6, 0.33f, R.color.common_text_black, R.color.common_text_black, 20);
        paypwd_resetpaypass_renew.initStyle(R.drawable.edit_num_bg, 6, 0.33f, R.color.common_text_black, R.color.common_text_black, 20);
    }

    @Override
    protected void initData() {

    }

    private void save() {
        boolean b = false;
        //new_payment_password
        String newPass = paypwd_resetpaypass_new.getPwdText();
        String newPass2 = paypwd_resetpaypass_renew.getPwdText();


        if (TextUtils.isEmpty(newPass) || !TextUtils.equals(newPass, newPass2)) {
            tv_resetpaypass_renew_error.setVisibility(View.VISIBLE);
            b = true;
        }
        if (b) {
            return;
        }
        getPresenter().changePassByOTP(newPass, otp);
    }

    @Override
    protected void initEvent() {

    }

    @OnClick({
            R.id.bt_resetpaypass_save
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_resetpaypass_save:
                save();
                break;
            default:
                break;
        }

    }

    @Override
    public void changePassResult(ResultModel resultModel) {
        if (resultModel.isSuccess()) {
            WToastHelper.showContentView(this, R.string.changepaypass_success);
            EventBus.getDefault().post(new ChangeProfileEvent(ChangeProfileType.ChangePayPass));
            finish();
        }
    }
}
